//
//  BQMyCarTool.m
//  BeeQuick
//
//  Created by mac on 16/11/22.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQMyCarTool.h"

@implementation BQMyCarTool



static id _instance;
+ (instancetype)shareMyCar
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

- (instancetype)init
{
    if(self = [super init])
    {
        self.goodArrM = [NSMutableArray array];
        self.CurrentGoodArrM = [NSMutableArray array];
    }
    return self;
}




//添加一个商品
- (void)addGoods:(BQHomeGoodModel *)model
{
    
    if([self.goodArrM containsObject:model])
    {
        //如果包含了这个model

        model.buyCount ++;
        
    }
    else
    {
        [self.goodArrM addObject:model];
        
//        if(model.Selected == YES)
//        {
//            [self.CurrentGoodArrM addObject:model];
//        }
//        
        model.buyCount ++;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil]; 
    
    
}

//删除一个商品
- (void)removeGoods:(BQHomeGoodModel *)model
{
    //先做减法  在做判断
    model.buyCount --;
    if(model.buyCount == 0)
    {
        [self.goodArrM removeObject:model];
        
        [self.CurrentGoodArrM removeObject:model];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
    
    
}
//
//返回总价格
//- (float)showTotalPrice
//{
//   __block CGFloat toatlPirce = 0;
//    
//    [self.goodArrM enumerateObjectsUsingBlock:^(BQHomeGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        
//        toatlPirce  += obj.price.floatValue * obj.buyCount;
//        
//    }];
//    
//    return toatlPirce;
//
//    
//}

//加强版 显示总价格
- (float)showTotalPrice
{
    __block CGFloat toatlPirce = 0;
    
    [self.goodArrM enumerateObjectsUsingBlock:^(BQHomeGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
 
        if(obj.Selected == YES)
        {
            toatlPirce  += obj.price.floatValue * obj.buyCount;
        }
        
    }];
    
    return toatlPirce;
    
    
}


////返回所有的购买商品数量
- (NSInteger)showTotalNum
{
   __block NSInteger totalNum = 0;
    
    [self.goodArrM enumerateObjectsUsingBlock:^(BQHomeGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        totalNum += obj.buyCount;
    
    }];
    
    return totalNum;
}



//清空自己的购物车
- (void)clearMyCar
{
    [self.goodArrM enumerateObjectsUsingBlock:^(BQHomeGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.buyCount = 0; //清除所有商品的购买数量
        
    }];
    
    [self.goodArrM removeAllObjects];
    
}


    
//返回用户购物车里面实际上选了的数量
//- (NSInteger)showNumWantToBuy
//{
//    __block NSInteger totalNum = 0;
//    
//    [self.goodArrM enumerateObjectsUsingBlock:^(BQHomeGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        if(obj.outSelected == NO)
//        {
//            totalNum += obj.buyCount;
//        }
//    }];
//    
//    return totalNum;
//}

//购物车里面实际上要买的种类
//- (NSInteger)showCategoryNumWantToBuy
//{
//    __block NSInteger totalCategoryNum = 0;
//    [self.goodArrM enumerateObjectsUsingBlock:^(BQHomeGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        if(obj.outSelected == NO)
//        {
//            totalCategoryNum++;
//        }
//    }];
//    
//    return totalCategoryNum;
//}



//除去一种类型的数据
- (void)deleteOneCategory:(BQHomeGoodModel*)model
{
    if([self.CurrentGoodArrM containsObject:model])//防蹦
    {
        [self.CurrentGoodArrM removeObject:model];
    }
}
//添加一种类型的数据
- (void)addOneCategory:(BQHomeGoodModel*)model
{
    
    [self.CurrentGoodArrM addObject:model];
}



//商品是否被全选
- (BOOL)isAllSelect
{
    int count = 0;
    
    for (BQHomeGoodModel *model in self.goodArrM) {
        if(model.Selected)
        {
            count++;
        }
    }
    if(count == self.goodArrM.count)
    {
        return YES;
    }
    else
    {
        return NO;
    }
    
    
}


//刷新购物车
- (void)reloadMyCarList
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:nil];
    
}

@end
